#-*- coding: UTF-8 -*-

from models import Server
from conn import session

def sqlcommit(mapdata):
    '''
    将数据写入数据库
    '''

    session.add(mapdata)  #在session中添加内容
    session.flush() #保存数据
    session.commit() #数据库事务的提交,sisson自动过期而不需要关闭

def sqlcommitall(mapdata):
    '''
    将数据写入数据库
    '''
    #regos = RegisterOs('123123','122.1.1.1')

    session.add_all(mapdata)  #在session中添加内容
    session.flush() #保存数据
    session.commit() #数据库事务的提交,sisson自动过期而不需要关闭
    
    
def workRegos(data):
    '''
    客户机注册
    '''
    server_sn = data['server_sn']
    server_height = data['server_height']
    server_asset_tag = data['server_asset_tag']
    
    server_cdrom = data['server_cdrom']
    server_raidtype = data['server_raidtype']
    comment = data['comment']
    is_virtual = data['is_virtual']

    
    #查询server_sn是否存在
    
    queryoid = session.query(Server).filter(Server.server_sn==server_sn).first() 
    #oid不存在,则进行insert操作
    if queryoid == None:
        
        sqlcommit(Server(server_sn, server_height, server_asset_tag,server_cdrom,server_raidtype,comment,is_virtual))
    else:
        queryoid.server_height = server_height
        queryoid.server_cdrom = server_cdrom
        queryoid.server_asset_tag = server_asset_tag
        queryoid.server_raidtype = server_raidtype
        queryoid.comment = comment
        queryoid.is_virtual = is_virtual
        session.commit() 

def workLostClient(server_sn):
    '''
    客户端断开连接
    '''
    
    connstatus = 1
    
    #查询oid是否存在，返回元祖(server_sn)
    queryoid = session.query(Server).filter(Server.server_sn==server_sn).first() 
    
    #aws服务器不进行oid注册
    if queryoid != None:
        #queryoid.connstatus = connstatus
        #session.commit() 
        pass
